package liuyao.webutils.controller;

import com.alibaba.fastjson.JSONObject;
import liuyao.webutils.utils.QRCodeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/qrcode")
@CrossOrigin
public class QRCodeController {

    @PostMapping("/analyze")
    public String analyze(MultipartFile file){
        // 解析二维码
        String analyze = QRCodeUtils.analyze(file);
        return analyze;
    }

    @PostMapping(value = "/generate")
    public String blobGenerate(@RequestBody JSONObject param, HttpServletResponse resp) {
        try {
            String contents = param.getString("contents");
            String result = "data:image/png;base64," + QRCodeUtils.generateBase64(contents);
            return result;
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

}
